package com.jdy.haoduoai.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.jdy.haoduoai.Constant;
import com.jdy.haoduoai.fragment.WelcomePageFragment;
import com.jdy.haoduoai.intface.SlideCallback;
import com.jdy.haoduoai.util.Tools;
import com.ycsj.goldmedalnewconcept.R;

import org.droidparts.annotation.inject.InjectView;

import java.util.ArrayList;
import java.util.List;

//import com.jdy.haoduoai.R;

public class ProductTourActivity extends BaseActivity {

  @InjectView(id = R.id.welcome_pager)
  private android.support.v4.view.ViewPager mWelcomePager;

  @InjectView(id = R.id.page_indicator_rg)
  private RadioGroup mPageIndicatorGroup;

  private WelcomePagerAdapter mWPA;

  private List<Integer> rbIds;

  private boolean isLeft = false; // 判断是否滑向左边
  private boolean isRight = false; // 判断是否滑向右边
  private boolean isScrolling = false; // 判断是否滑动中
  private int lastValue = -1; // 最后的位置
  private int mPos;

  SlideCallback sCallback = new SlideCallback() {

    boolean isLeft = false;
    boolean isRight = false;

    @Override
    public void getCurrentPageIndex(int index) {
      if (isLeft && !isRight) {

      } else if (isRight && !isLeft) {
        // Log.e(TAG, "右滑");
      }
    }

    @Override
    public void changeView(boolean isLeft, boolean isRight) {
      this.isLeft = isLeft;
      this.isRight = isRight;

      if (isLeft && !isRight) {
        if (mPos == 4) {

          SharedPreferences prefs = getSharedPreferences(Constant.WELCOME_PAGE,
              Context.MODE_PRIVATE);
          prefs.edit().putBoolean(Constant.HAS_ILLUSTRATED, true).apply();

          Intent i = new Intent(ProductTourActivity.this, SplashActivity.class);
          i.putExtra("comefromwelcomepage", 1);
          startActivity(i);
          finish();
        }
      }

    }
  };

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_tutorial);

    hideTitleView();

    buildCircles();

    mPageIndicatorGroup.check(rbIds.get(0));

    mWPA = new WelcomePagerAdapter(getSupportFragmentManager(), new int[] {
        R.drawable.welcome_1, R.drawable.welcome_2, R.drawable.welcome_3,
        R.drawable.welcome_4, R.drawable.welcome_5 });

    mWelcomePager.setOnPageChangeListener(new OnPageChangeListener() {

      @Override
      public void onPageSelected(int pos) {
        mPageIndicatorGroup.check(rbIds.get(pos));

        mPos = pos;
        if (sCallback != null) {
          sCallback.getCurrentPageIndex(pos);
        }
      }

      @Override
      public void onPageScrolled(int pos, float arg1, int arg2) {
        // pos :当前页面，及你点击滑动的页面
        // arg1:当前页面偏移的百分比
        // arg2:当前页面偏移的像素位置

        if (isScrolling) {
          if (lastValue > arg2) {
            // 递减，向右侧滑动
            isRight = true;
            isLeft = false;
          } else if (lastValue < arg2) {
            // 递减，向左侧滑动
            isRight = false;
            isLeft = true;
          } else if (lastValue == arg2) {
            isRight = isLeft = false;
          }
        }
        lastValue = arg2;
      }

      @Override
      public void onPageScrollStateChanged(int state) {
        // 有三种状态（0，1，2）。
        // state == 0 表示什么都没做。
        // state == 1 表示正在滑动
        // state == 2 表示滑动完毕了
        if (state == 1) {
          isScrolling = true;
        } else {
          isScrolling = false;
        }

        if (state == 1) {
          if (sCallback != null) {
            sCallback.changeView(isLeft, isRight);
          }
          isRight = isLeft = false;
        }
      }
    });

    mWelcomePager.setAdapter(mWPA);
  }

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    // TODO Auto-generated method stub
    // super.onSaveInstanceState(outState);
  }

  private void buildCircles() {

    rbIds = new ArrayList<Integer>();
    int i = 0;
    for (; i < 5; i++) {
      LayoutParams rbParams = new LayoutParams(
          LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

      rbParams.setMargins(Tools.getPixelByDip(this, 10), 0,
          Tools.getPixelByDip(this, 10), 0);

      RadioButton rb = new RadioButton(this);

      rb.setButtonDrawable(R.drawable.welcome_page_indicator_selector);

      rb.setId(i * 7);

      rb.setPadding(Tools.getPixelByDip(this, 5), 0,
          Tools.getPixelByDip(this, 5), 0);

      rb.setLayoutParams(rbParams);

      rbIds.add(rb.getId());

      mPageIndicatorGroup.addView(rb);
    }

  }

  private class WelcomePagerAdapter extends FragmentStatePagerAdapter {

    private int[] srcs;

    public WelcomePagerAdapter(FragmentManager fm) {
      super(fm);
    }

    public WelcomePagerAdapter(FragmentManager fm, int[] srcArray) {
      this(fm);
      srcs = srcArray;
    }

    @Override
    public int getItemPosition(Object object) {
      return POSITION_NONE;
    }

    @Override
    public Fragment getItem(int pos) {
      return WelcomePageFragment.newInstance(srcs[pos]);
    }

    @Override
    public int getCount() {
      return srcs.length;
    }
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
  }

}